{template 'common/header-storex'}
<div class="wn-main">
	<div class="wn-main__head">店员管理</div>
	<div class="wn-main__content">
		<div class="wn-tabs">
			<div class="wn-tabs__item wn-tabs__item--active">
			</div>
			<div class="wn-main__search__action">
				<a href="{php echo $this->createWebUrl('shop_clerk', array('op' => 'display', 'storeid' => $_GPC['storeid']));}" class="btn btn-primary we7-padding-horizontal">返回列表</a>
			</div>
		</div>
		<form action="" class="form-horizontal form" method="post" onsubmit="return formcheck()">
			<input type="hidden" name="id" value="{$item['id']}">
			<input type="hidden" name="storeid" value="{$_GPC['storeid']}">	
			<div class="panel-body">
				 <div class="form-group">
					<label class="col-xs-12 col-sm-3 col-md-2 control-label">姓名</label>
					<div class="col-sm-9 col-xs-12">
						<input type="text" name="realname" id="realname" value="{$item['realname']}" class="form-control">
					</div>
				</div>
				<div class="form-group">
					<label class="col-xs-12 col-sm-3 col-md-2 control-label">用户名</label>
					<div class="col-sm-9 col-xs-12">
						<input type="text" name="username" id="username" value="{$user_info['username']}" class="form-control" readonly>
					</div>
				</div>
				<div class="form-group">
					<label class="col-xs-12 col-sm-3 col-md-2 control-label">手机</label>
					<div class="col-sm-9 col-xs-12">
						<input type="text" name="mobile" id="mobile" value="{$item['mobile']}" class="form-control">
					</div>
				</div>
				<span style="display:none">
					<div class="form-group">
						<label class="col-xs-12 col-sm-3 col-md-2 control-label">{if !empty($id)}新{/if}密码</label>
						<div class="col-sm-9 col-xs-12">
							<input type="password" name="password" id="password" value="" class="form-control">
						</div>
					</div>
					<div class="form-group">
						<label class="col-xs-12 col-sm-3 col-md-2 control-label">确认{if !empty($id)}新{/if}密码</label>
						<div class="col-sm-9 col-xs-12">
							<input type="password" name="password2" id="password2" value="" class="form-control">
						</div>
					</div>
					{if !empty($item)}
					<div class="form-group">
						<label class="col-xs-12 col-sm-3 col-md-2 control-label">订房积分</label>
						<div class="col-sm-9 col-xs-12">
							<input type="text" name="score" id="score" value="{$item['score']}" class="form-control">
						</div>
					</div>
					{/if}
				</span>
				<div class="form-group">
					<label class="col-xs-12 col-sm-3 col-md-2 control-label">状态</label>
					<div class="col-sm-9 col-xs-12">
						<label class="radio-inline">
							<input type="radio" name="status" value="1" {if $item['status'] == 1}checked{/if}/>启用
						</label>
						<label class="radio-inline">
							<input type="radio" name="status" value="0" {if $item['status'] == 0}checked{/if}/>禁用
						</label>
						<span class='help-block'>禁用以后用户无法登录</span>
					</div>
				</div>
				<div class="form-group" style="display:none">
					<label class="col-xs-12 col-sm-3 col-md-2 control-label">身份</label>
					<div class="col-sm-9 col-xs-12">
						<label class="radio-inline">
							<input type="radio" name="clerk" value="1" {if $item['clerk'] == 1}checked{/if} onclick="$('#clerk_permission').show()"/>店员
						</label>
						<label class="radio-inline">
							<input type="radio" name="clerk" value="0" {if $item['clerk'] == 0}checked{/if} onclick="$('#clerk_permission').hide()"/>普通用户
						</label>
						<span class='help-block'>店员身份可以在手机端管理订单</span>
					</div>
				</div>
				<div class="form-group"  id="nickname" style="display: none;">
					<label class="col-xs-12 col-sm-3 col-md-2 control-label">微信昵称</label>
					<div class="col-sm-9 col-xs-12">
						<input type="text" name="nickname"  value="{$item['nickname']}" class="form-control">
						<span class='help-block'>请填写微信昵称，以便关联微信号</span>
					</div>
				</div>
				<div class="form-group"  id="from_user">
					<label class="col-xs-12 col-sm-3 col-md-2 control-label">微信openid</label>
					<div class="col-sm-9 col-xs-12">
						<input type="text" name="from_user"  value="{$item['from_user']}" class="form-control">
						<span class='help-block'>请填写该公众号下的微信openid，以便关联微信号</span>
					</div>
				</div>
				<div class="form-group"  id="from_user">
					<label class="col-xs-12 col-sm-3 col-md-2 control-label">卡券核销密码</label>
					<div class="col-sm-9 col-xs-12">
						<input type="text" name="password"  value="{$item['password']}" class="form-control">
						<span class='help-block'>请填写店员核销密码</span>
					</div>
				</div>
				<div class="form-group">
					<div class="panel panel-default">
						<div class="panel-heading">权限清单</div>
						<div class="panel-body table-responsive">
							{if !empty($user_permissions[$_GPC['uid']]['permission'])}
							{loop $user_permissions[$_GPC['uid']]['permission'] $permission}
								<div class="col-sm-3">
									<span class="">{$permission}</span>
								</div>
							{/loop}
							{/if}
						</div>
					</div>
				</div>
			</div>
			<div class="form-group col-sm-12">
				<input type="submit" name="submit" value="提交" class="btn btn-primary col-lg-1" />
				<input type="hidden" name="token" value="{$_W['token']}" />
			</div>
		</form>
	</div>
</div>
<div class="modal fade" id="store-modal" tabindex="-1" role="dialog" aria-hidden="true">
	<div class="modal-dialog">
		<div class="modal-content">
			<div class="modal-header">
				<button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
				<h4 class="modal-title" id="myModalLabel">选择你要添加的店铺权限</h4>
			</div>
			<div class="modal-body clearfix form-horizontal">

			</div>
			<div class="modal-footer">
				<button type="button" class="btn btn-default" data-dismiss="modal">取消</button>
				<button type="button" class="btn btn-primary" data-dismiss="modal" id="addPermission">确定</button>
			</div>
		</div>
	</div>
</div>
<script type="text/javascript">
	var stores = '<?php echo json_encode($stores);?>';
	var selected_stores = '<?php echo json_encode($store_lists);?>';
	stores = $.parseJSON(stores);
	selected_stores = $.parseJSON(selected_stores);

	var selected_pemissions = function(store_id, type) {
		if (type == 'stores') {
			store_lists = stores;
		} else if (type == 'selected_stores') {
			store_lists = selected_stores;
		}
		if (store_lists[store_id].store_type == 1) {
			store_description = '酒店';
		} else {
			store_description = '普通店铺';
		}
		permission_list = store_lists[store_id].permission;
		order_checked = '';
		room_checked = '';
		if ($.inArray("wn_storex_permission_order", permission_list) != '-1') {
			order_checked = 'checked=true';
		}
		if ($.inArray("wn_storex_permission_room", permission_list) != '-1') {
			room_checked = 'checked=true';
		}
		selected_store_list = '<div class="panel panel-default js-html-delete-' + store_id + '">'+
					'<div class="app-info panel-heading">'+
						'<img width="50px" src="'+store_lists[store_id].thumb+'" />'+
						'<span>'+store_lists[store_id].title+'</span>'+
						'<a href="javascript:;" class="pull-right js-permission-delete" data-id="' + store_id + '"><i class="fa fa-minus-circle"></i>删除</a>'+
					'</div>'+
				'<div class="panel-body" class="js-html-delete-' + store_id + '">'+
					'<input type="checkbox" class="permission" name="permission[' + store_id + '][]" id="permission[' + store_id + ']-1" value="wn_storex_permission_order" ' + order_checked + '"><label style="padding:0 10px;" for="permission[' + store_id + ']-1">订单管理</label>';
		if (store_lists[store_id].store_type == 1) {
			selected_store_list += '<input type="checkbox" class="permission" name="permission[' + store_id + '][]" id="permission[' + store_id + ']-2" value="wn_storex_permission_room" ' + room_checked + '"><label style="padding:0 10px;" for="permission[' + store_id + ']-2">房型管理</label>';
		}
		selected_store_list +='</div>'+
				'</div>';
		$('#permission').append(selected_store_list);
	}
	var selected_pemissions_init = function() {
		for (var i in selected_stores) {
			selected_pemissions(i, 'selected_stores');
			delete stores[i];
		}
	}
	selected_pemissions_init();
	$('#addPermission').click(function() {
		var storeid = $('#store-modal input[type=radio]:checked').val();
		selected_pemissions(storeid, 'stores');
		selected_stores[storeid] = stores[storeid];
		delete stores[storeid];
	});

	$('.js-permission-modal').click(function() {
		html = '';
		for (var i in stores) {
			if (stores[i].store_type == 1) {
				store_description = '酒店';
			} else {
				store_description = '普通店铺';
			}
			html += '<div class="form-group marbot0">'+
						'<label class="col-xs-12 col-sm-3 col-md-2 control-label"></label>'+
							'<div class="col-sm-9 col-xs-12">'+
								'<label class="radio-inline">'+
									'<input type="radio" name="type" value="' + i + '"/>'
									+ stores[i].title +
								'</label>'+
							'<div class="help-block">' + store_description + '</div>'+
							'</div>'+
					'</div>';
		}
		$('.modal-body').html(html);
		$('#store-modal').modal('show');
	});
	//删除模态框内标签
	$(document).on('click', '.js-permission-delete', function() {
		id = $(this).data('id');
		stores[id] = selected_stores[id];
		delete selected_stores[id];
		console.dir(stores);
		console.dir(selected_stores)
		$('.js-html-delete-' + id).remove();
	});
	function formcheck() {
		var isauto = $('input[name="isauto"]:checked').val();
		if (isauto == 0) {
			if ($("#realname").isEmpty()) {
				Tip.select("realname", "请填写姓名!", "right");
				return false;
			}
			if ($("#username").isEmpty()) {
				Tip.select("username", "请填写用户名!", "right");
				return false;
			}
			if (!$("#mobile").isMobile()) {
				Tip.select("mobile", "请填写正确的手机!", "right");
				return false;
			}
		}
		var check_pass = 1;
		{if !empty($id)}
			if ($("#password").isEmpty() && $("#password2").isEmpty()) {
				check_pass = 0;
			}
		{/if}
		if (check_pass) {
			if ($("#password").isEmpty()) {
				Tip.select("password", "请填写{if !empty($id)}新{/if}密码!", "right");
				return false;
			}
			if ($("#password2").isEmpty()) {
				Tip.select("password2", "请填写确认{if !empty($id)}新{/if}密码!", "right");
				return false;
			}
			if ($.trim($("#password").val()) != $.trim($("#password2").val())  ) {
				Tip.select("password2", "两次密码不一致，请确认!", "right");
				return false;
			}
		}
		return true;
	}
</script>
